<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户登录控制器
 * @author LM
 *
 */
class Login extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		//载入模型
		$this->load->service('login_service');
		$this->load->service('register_service');
		$this->load->library("form_validation");
		$this->config->load("form_validation");
	}
	
	//登录视图
	public function index(){
		$this->load->library('user_agent');
		if($this->agent->is_mobile()){
			redirect("/m/index.html#login");
		}
		//登录判断
		$is_login = $this->login_service->is_login();
		$is_tryout = $this->login_service->is_tryout();
		//如果已经登陆且不是使用账号则自动跳转到文件管理主页
		if($is_login && !$is_tryout){
			redirect('files');
		}

		//当没有手动登录时判断有无自动登录
		$auto = $this->login_service->auto_login();
		if($auto){
			redirect('files');
		}
		//错误5次以上出现验证码
		$show_vcode = $this->login_service->vcode_required();
		$this->template->assign('show_vcode', $show_vcode);

		#第三方登陆开关
		$social = $this->config->item('social');
		$this->template->assign('social', $social);

		$data = array(
				'title' => '登录'
				);
		$this->template->view('user/login.tpl', $data);
	}
	//登陆处理函数
	public function do_login()
	{
		//验证验证码
		$show_vcode = $this->login_service->vcode_required();
		$vcode = $this->session->userdata('__LOGIN_VCODE__');
		if($show_vcode){
			$code = $this->input->post('code',true);
			if(!$code || strtoupper($code) != $vcode)
			{
				return Common::ajax_return(1007,NULL,array("show_vcode"=>TRUE),FALSE);
			}
		}

		//验证邮箱和密码
		$account = trim($this->input->post('account',true));
		$email_valid = $this->form_validation->valid_email($account);
		$phone_valid = $this->form_validation->valid_mobile_phone($account);
		if(!$account || !( $email_valid || $phone_valid))
		{
			return Common::ajax_return(1003);
		}
		$password = $this->input->post('password',true);
		if(!$password)
		{
			return Common::ajax_return(1003);
		}
		$user =$email_valid?$this->login_service->check_login_by_email($account,$password):$this->login_service->check_login_by_phone($account,$password);
		//$num = $this->login_service->get_login_error_num();
		$extra = array("redirect"=>"/files");
		if($user===false)
		{
			//加入错误日志
			$error = $this->login_service->get_error();
			$this->login_service->add_login_error_log();
			if($error['code']===5006 && $email_valid)
			{
				$host = Common::get_email_url($account);
				$extra['host'] = $host;
			}
			else if($error['code']==5002 && $this->login_service->vcode_required())
			{
				$extra['show_vcode'] = true;
			}
			return Common::ajax_return($this->login_service,NULL,$extra,FALSE);
		}
		else
		{
			//如果试用已经登陆，则先销毁登录信息
			if($this->login_service->is_tryout() && $this->login_service->is_login())
			{
				$this->login_service->logout();
			}
			//选择了下次自动登录
			if($this->input->post('auto_login',TRUE)){
				$this->login_service->add_auto_login($user['id']);
			}
			$this->login_service->login_by_data($user,0,0);
			return Common::ajax_return(0,NULL,$extra,FALSE);
		}

	}

	public function check_email()
	{
		$this->form_validation->set_rules('email','', 'required|valid_email');
		if($this->form_validation->run()==FALSE)
		{
			return Common::ajax_return(1003);
		}
		$email = trim($this->input->post('email',true));
		$userData = $this->register_service->check_email_exists($email);
		if($userData){
			if($userData['verify'] == 0){
				if($this->register_service->check_overdue_by_email($email)){
					return Common::ajax_return();
				} else {
					$data = Common::get_email_url($email);
					//self::ret_error(3, '该邮箱已被注册但没有激活', $data);
					return Common::ajax_return(5010,NULL,array("host"=>$data,"email"=>$email),FALSE);
				}
			}
			return Common::ajax_return();
		}
		return Common::ajax_return();
	}

	//生成验证码
	public function code(){
		$config = array(
				'width'	=>	80,
				'height'=>	34,
				'codeLen'=>	4,
				'fontSize'=>16
		);
		$this->load->library('code', $config);
		$this->session->set_userdata('__LOGIN_VCODE__', $this->code->getCode());
		$this->code->show();
	}

	//退出登录
	public function logout(){
		$this->login_service->logout();
		redirect('login');
	}

	//立即试用
	public function run_tryout(){
		/*$try = $this->login_service->tryout();
		//var_dump($this->login_service->get_error());
		if($try)
		{
			redirect('files');
		}
		else
		{
			show_404();
		}*/
	}

	/**
	 * 立即试用
	 */
	public function tryout(){
		/*if(!$this->login_service->get_tryout()){
			redirect('/helpcenter/protocol#tryout');
		} else {
			redirect('/login/run_tryout');
		}*/
	}
	
	/**
	 * 错误处理
	 */
	static function ret_error($code = 1, $error = '', $data = null){
		$data = array(
				'ret'       => $code,
				'error'     => $error,
				'data'      => $data
		);
		echo json_encode($data);
		die();
	}


	
	
}